﻿using System;
using Mono.Collections.Generic;

namespace Mono.Cecil
{
	// Token: 0x02000086 RID: 134
	internal struct ImportGenericContext
	{
		// Token: 0x1700010C RID: 268
		// (get) Token: 0x060004FF RID: 1279 RVA: 0x0001CFC0 File Offset: 0x0001B1C0
		public bool IsEmpty
		{
			get
			{
				return this.stack == null;
			}
		}

		// Token: 0x06000500 RID: 1280 RVA: 0x0001CFDC File Offset: 0x0001B1DC
		public ImportGenericContext(IGenericParameterProvider provider)
		{
			bool flag = provider == null;
			if (flag)
			{
				throw new ArgumentNullException("provider");
			}
			this.stack = null;
			this.Push(provider);
		}

		// Token: 0x06000501 RID: 1281 RVA: 0x0001D00C File Offset: 0x0001B20C
		public void Push(IGenericParameterProvider provider)
		{
			bool flag = this.stack == null;
			if (flag)
			{
				this.stack = new Collection<IGenericParameterProvider>(1)
				{
					provider
				};
			}
			else
			{
				this.stack.Add(provider);
			}
		}

		// Token: 0x06000502 RID: 1282 RVA: 0x00003DB7 File Offset: 0x00001FB7
		public void Pop()
		{
			this.stack.RemoveAt(this.stack.Count - 1);
		}

		// Token: 0x06000503 RID: 1283 RVA: 0x0001D04C File Offset: 0x0001B24C
		public TypeReference MethodParameter(string method, int position)
		{
			for (int i = this.stack.Count - 1; i >= 0; i--)
			{
				MethodReference methodReference = this.stack[i] as MethodReference;
				bool flag = methodReference == null;
				if (!flag)
				{
					bool flag2 = method != this.NormalizeMethodName(methodReference);
					if (!flag2)
					{
						return methodReference.GenericParameters[position];
					}
				}
			}
			throw new InvalidOperationException();
		}

		// Token: 0x06000504 RID: 1284 RVA: 0x0001D0C4 File Offset: 0x0001B2C4
		public string NormalizeMethodName(MethodReference method)
		{
			return method.DeclaringType.GetElementType().FullName + "." + method.Name;
		}

		// Token: 0x06000505 RID: 1285 RVA: 0x0001D0F8 File Offset: 0x0001B2F8
		public TypeReference TypeParameter(string type, int position)
		{
			for (int i = this.stack.Count - 1; i >= 0; i--)
			{
				TypeReference typeReference = ImportGenericContext.GenericTypeFor(this.stack[i]);
				bool flag = typeReference.FullName != type;
				if (!flag)
				{
					return typeReference.GenericParameters[position];
				}
			}
			throw new InvalidOperationException();
		}

		// Token: 0x06000506 RID: 1286 RVA: 0x0001D164 File Offset: 0x0001B364
		private static TypeReference GenericTypeFor(IGenericParameterProvider context)
		{
			TypeReference typeReference = context as TypeReference;
			bool flag = typeReference != null;
			TypeReference elementType;
			if (flag)
			{
				elementType = typeReference.GetElementType();
			}
			else
			{
				MethodReference methodReference = context as MethodReference;
				bool flag2 = methodReference != null;
				if (!flag2)
				{
					throw new InvalidOperationException();
				}
				elementType = methodReference.DeclaringType.GetElementType();
			}
			return elementType;
		}

		// Token: 0x06000507 RID: 1287 RVA: 0x0001D1B0 File Offset: 0x0001B3B0
		public static ImportGenericContext For(IGenericParameterProvider context)
		{
			return (context != null) ? new ImportGenericContext(context) : default(ImportGenericContext);
		}

		// Token: 0x04000142 RID: 322
		private Collection<IGenericParameterProvider> stack;
	}
}
